package com.to.aboomy.bannersample.component;

import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.adapter.BannerAdapter;
import com.to.aboomy.bannersample.adapter.FragmentBannerAdapter;
import com.to.aboomy.bannersample.bean.BannerBean;
import com.to.aboomy.bannersample.bean.TextBean;
import com.to.aboomy.bannersample.util.Utils;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author xujianhong
 * @since 2021-07-22
 */
public class BannerAndList extends DirectionalLayout {

    FragmentBannerAdapter bannerAdapter;
    public BannerAndList(Context context) {
        this(context,null);
    }

    public BannerAndList(Context context, AttrSet attrSet) {
        this(context, attrSet,null);
    }

    public BannerAndList(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        bannerAdapter = new FragmentBannerAdapter(mContext);
        Component rootView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_fragment_test_item, this, true);
        ListContainer list = (ListContainer) rootView.findComponentById(ResourceTable.Id_list);
        loadData();
        list.setItemProvider(bannerAdapter);
    }

    private void loadData() {
        List<Object> list = new ArrayList<>();
        BannerBean bannerBean = new BannerBean();
        bannerBean.urls = new ArrayList<>();
        bannerBean.itemType = 4;
        int count = new Random().nextInt(4) + 1;
        for (int i = 0; i < count; i++) {
            bannerBean.urls.add(Utils.getRandomImage());
        }
        list.add(bannerBean);

        for (int i = 0; i < 200; i++) {
            TextBean textBean = new TextBean();
            textBean.text = "--- " + i;
            list.add(textBean);
        }
        bannerAdapter.setData(list);
    }
}
